﻿using SuperMap.Data;
using SuperMap.Realspace;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic.Conditions;
using Ycjw.PipeLine.Map.Basic.SuperMap;

namespace Ycjw.PipeLine.Map.Tools.Conditions.Statistics
{
    public partial class GJZHStatistics : ConditionPanel
    {
        private LayerWithDataListPanel LayerWithDataListPanel;
        public GJZHStatistics() : base()
        {
            this.SetSize(300, 200);
            this.ConditionPanelName = "管线综合统计";
            InitializeComponent();
        }
        private readonly string GJGXXZFIELDNAME = "管线性质";
        protected override void OnLoad(EventArgs e)
        {
            LayerWithDataListPanel = new LayerWithDataListPanel()
            {
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            Scene scene = this.Scene;
            if (scene == null) return;
            LayerWithDataListPanel.Init(scene);
            //LayerWithDataListPanel.LoadToVector();
            var types = new List<DatasetType>()
            {
                DatasetType.Line,
                DatasetType.Line3D ,
                DatasetType.Network,
                DatasetType.Network3D
            };
            LayerWithDataListPanel.LoadTo(types, true);
            this.uiGroupBoxTJTC.Controls.Add(LayerWithDataListPanel);
        }

        public override object Do(DockPanel dockPanel = null)
        {
            DataTable dt = new DataTable();
            dt.TableName = ConditionPanelName;
            dt.Columns.Add("系列");

            dt = StatisticsByAll(dt);
            return dt;
        }

        public override bool ConditionValid()
        {
            if (this.LayerWithDataListPanel.SelectLayer3DDatasets.Count == 0)
                return false;
            return true;
        }

        private DataTable StatisticsByAll(DataTable dt)
        {
            List<Layer3DDataset> datasets = this.LayerWithDataListPanel.SelectLayer3DDatasets;
            List<Dictionary<string, double>> results = new List<Dictionary<string, double>>();
            foreach (Layer3DDataset layer3DDataset in datasets)
            {
                var dataSetVector = layer3DDataset.Dataset as SuperMap.Data.DatasetVector;
                if (dataSetVector.FieldInfos.IndexOf(GJGXXZFIELDNAME) < 0) continue;
                DatasetVectorEx dvEx = new DatasetVectorEx(layer3DDataset.Dataset.Name);
                dvEx.DatasetVector = dataSetVector;
                var result = dvEx.UniqueValLength(GJGXXZFIELDNAME);

                results.Add(result);

                foreach (var key in result.Keys)
                {
                    var tmpKey = key;
                    if (string.IsNullOrEmpty(key))
                        tmpKey = "未知";
                    if (!dt.Columns.Contains(tmpKey))
                        dt.Columns.Add(tmpKey);
                }
            }
            foreach (Layer3DDataset layer3DDataset in datasets)
            {
                var dr = dt.NewRow();
                dr[0] = layer3DDataset.Caption;
                dt.Rows.Add(dr);
            }
            var rowIndex = 0;
            foreach (var r in results)
            {
                var dr = dt.Rows[rowIndex++];
                foreach (var key in r.Keys)
                {
                    var tmpKey = key;
                    if (string.IsNullOrEmpty(key))
                        tmpKey = "未知";
                    dr[tmpKey] = r[key];
                }
            }
            return dt;
        }

    }
}
